package com.wl.rjwmc.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wl.rjwmc.DoMain.Orders;
import com.wl.rjwmc.DoMain.User;
import com.wl.rjwmc.Service.OrdersService;
import com.wl.rjwmc.Utools.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @GetMapping("page")
    public R<Page<Orders>> list(int page,int pageSize,String number, String beginTime, String endTime){
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.like(number != null,Orders::getId, number);
        lqw.ge(beginTime != null,Orders::getOrderTime,beginTime);
        lqw.le(endTime != null,Orders::getOrderTime,endTime);
        lqw.orderByAsc(Orders::getOrderTime);
        ordersService.page(ordersPage,lqw);
        List<Orders> records = ordersPage.getRecords();
        records = records.stream().map((item) -> {
            item.setUserName(""+item.getUserId());
            return item;
        }).collect(Collectors.toList());
        return R.success(ordersPage);
    }

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);
        ordersService.submit(orders);
        return R.success("下单成功");
    }
}

